package com.simple.datax.dao.impl;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.simple.base.dao.BaseDAO;
import com.simple.datax.dao.IDataxDictionaryDAO;
import com.simple.datax.vo.DataxDictionaryIdVO;
import com.simple.datax.vo.DataxDictionaryVO;
import com.simple.util.common.Constants;
import com.simple.util.common.StringUtil;
import com.simple.util.common.Utils;
import com.simple.util.object.PageInfoBean;

@Repository("dataxDictionaryDAO")
public class DataxDictionaryDAOImpl extends BaseDAO implements IDataxDictionaryDAO {

	@Override
	public DataxDictionaryVO findById(final DataxDictionaryIdVO id) {
		return  super.get(DataxDictionaryVO.class,id);
	}

	@Override
	public void saveOrUpdate(final DataxDictionaryVO dataxDictionaryVO) {
		super.saveOrUpdate(dataxDictionaryVO);
	}

	@Override
	public List<DataxDictionaryVO> findAll() {
		return super.query(DataxDictionaryVO.class);
	}
	

	@Override
	public void delete(final DataxDictionaryVO dataxDictionaryVO) {
		super.delete(dataxDictionaryVO);
	}
	
	
	@SuppressWarnings("unchecked")
	@Override
	public List<DataxDictionaryVO> findDataxDictionaryVOByPage(final PageInfoBean pageInfoBean, final String fcCode, final String fcName ,final String fiCatagory) {
		final Map<String, Object> paramMap = new LinkedHashMap<String,Object>();
        final StringBuffer sb = new StringBuffer(300);
        
        sb.append( "select  d from DataxDictionaryVO d where 1>0 " );
        if(!StringUtil.isNull(fcCode)){
            sb.append( " and d.id.fcCode=:fcCode" );
            paramMap.put("fcCode", fcCode);
        }
        if(!StringUtil.isNull(fcName)){
            sb.append( " and d.fcName=:fcName" );
            paramMap.put("fcName", fcName);
        }
        if(!StringUtil.isNull(fiCatagory)){
            sb.append( " and d.id.fiCatagory=:fiCatagory" );
            paramMap.put("fiCatagory", Utils.parseInt(fiCatagory));
        }
        sb.append( " order by d.id.fcCode desc" );
        return (List<DataxDictionaryVO>) super.queryListByPage("d.id.fcCode", sb.toString(), paramMap, pageInfoBean);
		
	}

	@Override
	public List<DataxDictionaryVO> findDataxDictionaryVOByCatagory(
			String fiCatagory) {
        final StringBuffer sb = new StringBuffer(300);
        
        sb.append( "select  d from DataxDictionaryVO d where 1>0 " );
        if(!StringUtil.isNull(fiCatagory)){
            sb.append( " and d.id.fiCatagory=?" );
        }
        sb.append( " order by d.id.fcCode asc" );
		return  super.query(sb.toString(), Utils.parseInt(fiCatagory));
	}

	@Override
	public List<DataxDictionaryVO> findDataxDictionaryVOByEnterprise(
			String eCode) {
		
		String hql ="select d  from DataxDictionaryVO d , DataxEnterpriseChannelVO e "
					+" where 1>0 and d.id.fcCode=e.id.fcServicecode and e.id.fcEntcode=? and d.id.fiCatagory="
					+Integer.parseInt(Constants.TRANS_PROPERTY_FICATAGORY_SERVECODE)
					+" order by d.fcName asc";
		return super.query(hql, eCode);
	}

	@Override
	public List<DataxDictionaryVO> findTransServiceTypeDataxDictionaryVO() {
		String hql ="from DataxDictionaryVO d  where 1>0 and d.id.fiCatagory="+Integer.parseInt(Constants.TRANS_PROPERTY_FICATAGORY_SERVECODE);
		return super.query(hql);
	}

}
